package cn.josh.daolmpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.josh.bean.Ec_order;
import cn.josh.dao.Ec_orderDao;
import cn.josh.utill.JDBCUtil;

public class Ec_orderDaoImpl implements Ec_orderDao {

	@Override
	public int insertOrder(Ec_order order) 
	{
		String sql = "INSERT INTO ec_order (order_code,create_date,STATUS,AMOUNT,user_Id)"
				+ " VALUE (?,?,?,?,?);";
		String info[] = {order.getOrderCode(),order.getCreateDate(),order.getStatus(),order.getAmount(),order.getUserId()};
		int in = JDBCUtil.executeUpdate(sql, info);
		
		return in;
	}

	@Override
	public List<Ec_order> findAll(Ec_order order) 
	{
		String sql = "SELECT * FROM ec_order WHERE user_Id = ?;";
		String[] info = {order.getUserId()};
		ResultSet rs = JDBCUtil.executeQuery(sql, info);
		List<Ec_order> orderList = new ArrayList<Ec_order>();
		try 
		{
			while(rs.next())
			{
				Ec_order orderBack = new Ec_order();
				orderBack.setOrderId(rs.getString("id"));
				orderBack.setOrderCode(rs.getString("order_code"));
				orderBack.setCreateDate(rs.getString("create_date"));
				orderBack.setStatus(rs.getString("status"));
				orderBack.setAmount(rs.getString("amount"));
				
				orderList.add(orderBack);
			}
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return orderList;
	}


	public int deleteOrder(Ec_order order) 
	{
		String sql = "DELETE FROM ec_order WHERE user_Id = ? AND Id = ?;";
		String[] info = {order.getUserId(),order.getOrderId()};
		System.out.println("Ec_shopcarDaoImpl 将要删除的订单id是：" + order.getOrderId());
		System.out.println("用户id是：" + order.getUserId());
		int in = JDBCUtil.executeUpdate(sql, info);
		return in;
	}

}
